﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class TabGeneral : Base
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            SetControls();

        }

        private void SetControls()
        {
            if (Page.IsPostBack)
                return;

            //this.ddlStatus.SelectedValue = this.CurrentDraw.status;
            this.CalendarDrawDate.CalendarValue = this.CurrentDraw.drawdate.ToString("dd MMMM yyyy");

            this.litStatus.Text = this.CurrentDraw.status;
            this.litDrawDate.Text = this.CurrentDraw.drawdate.ToString("dd MMMM yyyy");
            this.litExtraFunds.Text = String.Format("&pound;{0:0.00}", this.CurrentDraw.extraFunds);
            this.txtExtraFunds.Text =  String.Format("{0:0.00}", this.CurrentDraw.extraFunds);
            this.txtWinnings.Text = String.Format("{0:0.00}", this.CurrentDraw.winnings);

            if (this.CurrentDraw.status == draw.drawstatus.OPEN)
            {
                this.CalendarDrawDate.Visible = true;
                this.litDrawDate.Visible = false;
                this.ButtonSave.Visible = true;
                this.ButtonCheckResult.Visible = false;
                this.PlaceHolderResult.Visible = false;
                this.litStatus.Visible = true;
                this.ddlStatus.Visible = false;
                this.litExtraFunds.Visible = false;
                this.txtExtraFunds.Visible = true;
                this.txtWinnings.Visible = false;
            }
            else if (this.CurrentDraw.status == draw.drawstatus.PENDING)
            {
                this.CalendarDrawDate.Visible = false;
                this.litDrawDate.Visible = true;
                this.ButtonSave.Visible = false;
                this.ButtonCheckResult.Visible = true;
                this.PlaceHolderResult.Visible = false;
                this.ddlStatus.Visible = false;
                this.litStatus.Visible = true;
                this.litExtraFunds.Visible = true;
                this.txtExtraFunds.Visible = false;
                this.txtWinnings.Visible = false;
            }
            else
            {
                this.CalendarDrawDate.Visible = false;
                this.litDrawDate.Visible = true;
                this.ButtonSave.Visible = true;
                this.ButtonCheckResult.Visible = false;
                this.PlaceHolderResult.Visible = true;
                this.ddlStatus.Visible = false;
                this.litStatus.Visible = true;
                this.litExtraFunds.Visible = true;
                this.txtExtraFunds.Visible = false;
                this.txtWinnings.Visible = true;
            }
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            draw d = this.BasePage.BusinessFacade.DrawBL.GetSingleById(CurrentDraw.id);

            if (!this.DrawLocked)
            {
                d.drawdate = DateTime.Parse(this.CalendarDrawDate.CalendarValue);
                double r = 0;
                if (Double.TryParse(this.txtExtraFunds.Text, out r))
                    d.extraFunds = r;
            }
            else if (!this.DrawFinished)
            {

                double r = 0;
                if (Double.TryParse(this.txtExtraFunds.Text, out r))
                    d.extraFunds = r;

            }
            else if (this.DrawFinished)
            { 
                double r = 0;
                if (Double.TryParse(this.txtWinnings.Text, out r))
                   d.winnings  = r;

            
            }

            this.BasePage.BusinessFacade.DrawBL.UpdateDraw();

            InsertEventLogEntry("Draw [" + CurrentDraw.id.ToString() + "] updated.");
        }

        protected void ButtonCheckResult_Click(object sender, EventArgs e)
        {
            this.BasePage.BusinessFacade.ResultBL.FindResults(this.BasePage.LoggedInUser.syndicateId);
        }

        public string PrintStats()
        {
            var list = new List<BallStat>();

            for (int i = 1; i <= 50; i++ )
            {
               list.Add(new BallStat() {Number = i});
            }


            var q = this.CurrentDraw.lines;

            foreach (var line in q)
            {
                var numbers = line.numbers.Split(',').ToList().Select(x => Int32.Parse(x));

                foreach (var number in numbers)
                {
                    var ballset = list.SingleOrDefault(x => x.Number == number);

                    if (ballset != null)
                        ballset.Count++;
                    else
                    {
                        list.Add(new BallStat(){Number = number, Count = 1});
                    }
                }
            }

            StringBuilder builderTop = new StringBuilder();

            builderTop.Append("<th>Number:</th>");
           
            StringBuilder builderBottom = new StringBuilder();

            builderBottom.Append("<td>Count:</td>");

            foreach (var ballset in list.OrderBy(x=>x.Number))
            {
                builderTop.Append(String.Format("<th>{0}</th>", ballset.Number));
                builderBottom.Append(String.Format("<td>{0}</td>", ballset.Count));
            }

            return String.Format("<table style=\"padding:3px;\"><tr>{0}</tr><tr>{1}</tr></table>", builderTop.ToString(),builderBottom.ToString());

        }



    }

    public class BallStat
    {
        public int Number { get; set; }
        public int Count { get; set; }

        public BallStat()
        {
            this.Count = 0;
        }

    }
}